package org.itboys.mobile.service.mysql.site;

import org.apache.commons.lang.StringUtils;
import org.itboys.admin.dto.lasted.SundayUploadDto;
import org.itboys.admin.entity.lasted.SystemDictionary;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.admin.service.lasted.SystemDictionaryService;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.product.SundayProductMapper;
import org.itboys.mobile.entity.mongo.JoinFavorite;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mongo.SundayMemberRecord;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.entity.mysql.product.SundayProductCategory;
import org.itboys.mobile.entity.mysql.product.SundayProductRecord;
import org.itboys.mobile.service.mongo.site.JoinSiteFavoriteService;
import org.itboys.mobile.service.mongo.site.SundaySiteMemberRecordService;
import org.itboys.mobile.service.mongo.site.SundaySiteMemberService;
import org.itboys.mobile.service.mongo.web.SundayPermissionService;
import org.itboys.mobile.service.mysql.web.product.SundayProductCategoryService;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.provider.Sun;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/10/9
 * 描述：雄安加盟网_项目
 */
@Service
public class SundaySiteProductService extends BaseMySqlService<SundayProduct>{
    @Autowired
    private SystemDictionaryService dictionaryService;
    @Autowired
    private SundaySiteMemberService memberService;
    @Autowired
    private SundayPermissionService permissionService;
    @Autowired
    private JoinSiteFavoriteService favoriteService;
    @Autowired
    private SundaySiteMemberRecordService memberRecordService;
    @Autowired
    private SundayProductMapper productMapper;
    @Autowired
    private SundayProductCategoryService categoryService;
    @Autowired
    private SundayUploadService uploadService;
    /*    public void updateData(Long productId,String joinFee,String joinAdvantage,String joinProcess){
            SundayProduct product=super.find(productId);
            product.setJoinFee(joinFee);
            product.setJoinAdvantage(joinAdvantage);
            product.setJoinProcess(joinProcess);
            super.update(product);
        }*/
    public SundayProduct findOne(Long productId){
        SundayProduct product=super.find(productId);
        //组装轮播图
        List<SundayUploadDto> rollImages = uploadService.selectNoPage(productId,SundayProduct.class.getSimpleName());
        product.setRollImages(rollImages);
        return product;
    }
    public SundayProduct packageTags(SundayProduct product,  boolean isAll){

        //组装项目招商模式标签
        List<SystemDictionary> joinModelDictionaries=dictionaryService.getDictionaryByKey("join_model");
            if(product.getJoinModel()==null)product.setJoinModel("");
            String [] joinModelTagIds = product.getJoinModel().split(",");

            List<Map<String,Object>> joinModelTags = new ArrayList<Map<String, Object>>();
            for(SystemDictionary dictionary:joinModelDictionaries){
                Map<String,Object> tag=new HashMap<String,Object>();
                tag.put("id",dictionary.getId());
                tag.put("name",dictionary.getValue());
                int isChecked=0;

                for(String modelTagId:joinModelTagIds){
                    if(modelTagId.equalsIgnoreCase(String.valueOf(dictionary.getId()))){
                        isChecked=1;
                        break;
                    }
                }
                tag.put("isChecked",isChecked);
                if(!isAll){
                    if(isChecked==1)joinModelTags.add(tag);
                }else{
                    joinModelTags.add(tag);
                }

            }
            product.setJoinModelTags(joinModelTags);

        //组装项目经营模式标签
        List<SystemDictionary> managerModelDictionaries=dictionaryService.getDictionaryByKey("manager_model");
        if(product.getManagerModel()==null)product.setManagerModel("");
        String [] managerModelTagIds = product.getManagerModel().split(",");

        List<Map<String,Object>> managerModelTags = new ArrayList<Map<String, Object>>();
        for(SystemDictionary dictionary:managerModelDictionaries){
            Map<String,Object> tag=new HashMap<String,Object>();
            tag.put("id",dictionary.getId());
            tag.put("name",dictionary.getValue());
            int isChecked=0;

            for(String managerTagId:managerModelTagIds){
                if(managerTagId.equalsIgnoreCase(String.valueOf(dictionary.getId()))){
                    isChecked=1;
                    break;
                }
            }
            tag.put("isChecked",isChecked);
            if(!isAll){
                if(isChecked==1)managerModelTags.add(tag);
            }else{
                managerModelTags.add(tag);
            }
        }
        product.setManagerModelTags(managerModelTags);


        //组装项目适合人群标签
        List<SystemDictionary> fitPersonDictionaries=dictionaryService.getDictionaryByKey("fit_person");
        if(product.getFitPerson()==null)product.setFitPerson("");
        String [] fitPersonTagIds = product.getFitPerson().split(",");

        List<Map<String,Object>> fitPersonTags = new ArrayList<Map<String, Object>>();
        for(SystemDictionary dictionary:fitPersonDictionaries){
            Map<String,Object> tag=new HashMap<String,Object>();
            tag.put("id",dictionary.getId());
            tag.put("name",dictionary.getValue());
            int isChecked=0;

            for(String fitPersonTagId:fitPersonTagIds){
                if(fitPersonTagId.equalsIgnoreCase(String.valueOf(dictionary.getId()))){
                    isChecked=1;
                    break;
                }
            }
            tag.put("isChecked",isChecked);
            if(!isAll){
                if(isChecked==1)managerModelTags.add(tag);
            }else{
                fitPersonTags.add(tag);
            }
        }
        product.setFitPersonTags(fitPersonTags);

        return product;
    }

    /**
     * 根据企业返回该企业的项目
     * @param memberId
     * @return
     */
    public SundayProduct findOneByMember(Long memberId){
        Long productId=0l;
        SundayMember member=memberService.getById(memberId);
        List<Long> otherIds=permissionService.getPermission(memberId,1);
        if(ListUtil.isNotNull(otherIds)){
            productId=otherIds.get(0);
        }else {
            throw  new ErrorException("系统异常");
        }

        return super.find(productId);
    }

    /**
     * 根据记录id返回项目
     * @param productRecordId
     * @return
     */
    public Long getIdByRecord(Long productRecordId){
        if(LongUtil.isZreo(productRecordId)){
            return 0l;
        }
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("recordId",productRecordId);
        List<SundayProduct> products=super.select(param);
        if(ListUtil.isNotNull(products)){
            return products.get(0).getId();
        }else {
            return 0l;
        }
    }

    /**
     * 根据分类分组   获取前六个数据
     * @return
     */
    public List<SundayProduct> selectNewSix(){

        List<SundayProduct> products=productMapper.selectNewSix();

        return products;

    }


    /**
     * 获取所有分类下的最新6个
     * @return
     */
    public List<SundayProduct> selectAllNewSix(){
        Map<String,Object> param=new HashMap<String,Object>();
        param.clear();
        param.put("sort_name","ct");
        param.put("sort_order","desc");
        param.put("row_start",0);
        param.put("row_size",6);
        return super.select(param);
    }

    /**
     * 获取品牌优选  获取前六个数据
     * @param categoryIds
     * @return
     */
    public List<SundayProduct> selectGoodSix(List<Long> categoryIds){
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted",0);
        param.put("categoryIds",categoryIds);
        param.put("isBrandGood",1);
        param.put("row_start",0);
        param.put("row_size",6);
        List<SundayProduct> products=super.select(param);
        return products;

    }

    /**
     * 获取排名项目
     * @param size
     * @param parentId
     * @return
     */
    public List<SundayProduct> selectPaiming(Integer size,Long parentId){
        Map<String,Object> param=new HashMap<String,Object>();
        if(LongUtil.isNotZreo(parentId)){
            param.put("parentId",parentId);
        }
        param.put("row_start",0);
        param.put("row_size",size);
        param.put("sort_name","sequence");
        param.put("sort_order","desc");
        return super.select(param);
    }

    /**
     * 获取好项目
     * @param parentId
     * @return
     */
    public List<SundayProduct> selectGood(Long parentId){
        Map<String,Object> param=new HashMap<String,Object>();
        if(LongUtil.isNotZreo(parentId)){
            param.put("parentId",parentId);
        }
        param.put("isGood",1);
        param.put("row_start",0);
        param.put("row_size",5);
        param.put("sort_name","ct");
        param.put("sort_order","desc");
        return super.select(param);
    }

    /**
     * 获取相关项目
     * @param id
     * @return
     */
    public List<SundayProduct> selectXiangguan(Long id){
        Map<String,Object> param=new HashMap<String,Object>();
        SundayProduct product=super.find(id);
        param.put("categoryId",product.getCategoryId());
        param.put("id",id);
        param.put("row_start",0);
        param.put("row_size",4);
        param.put("sort_name","ct");
        param.put("sort_order","desc");
        return super.select(param);

    }



    /**
     * 查询页面查询项目
     * @param parentId
     * @param categoryId
     * @param investAmount
     * @param brandSource
     * @param name
     * @param page
     * @return
     */
    public PageResult<SundayProduct> searchProduct(Long parentId, Long categoryId, String investAmount, String brandSource, String name, Integer page){
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted",0);
        if(LongUtil.isNotZreo(parentId)){
            param.put("parentId",parentId);
        }
        if(LongUtil.isNotZreo(categoryId)){
            param.put("categoryId",categoryId);
        }
        if(StringUtils.isNotBlank(investAmount)){
            param.put("investAmount",investAmount);
        }
        if(StringUtils.isNotBlank(brandSource)){
            param.put("brandSource",brandSource);
        }
        if(StringUtils.isNotBlank(name)){
            param.put("name",name);
        }
        if(page!=null&&page!=0){
            param.put("row_start",(page-1)* SundayCommonConstants.row_size);
        }else {
            param.put("row_start",0);
    }
        param.put("row_size",SundayCommonConstants.row_size);

         return new PageResult<>(super.select(param),super.count(param));

    }

    /**
     * 获取首页项目
     * @return
     */
    public Map<String,Object> selectIndexProduct(){
        Map<String,Object> result=new HashMap<String,Object>();
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted",0);
        param.put("isIndexHot",1);
        param.put("row_start",0);
        param.put("row_size",12);
        List<SundayProduct> hotProducts=super.select(param);
        result.put("hotProducts",hotProducts);
        param.clear();
        param.put("isDeleted",0);
        param.put("isIndexRecommend",1);
        param.put("row_start",0);
        param.put("row_size",16);
        List<SundayProduct> recommendProducts=super.select(param);
        result.put("recommendProducts",recommendProducts);
        param.clear();
        param.put("isDeleted",0);
        param.put("isIndexText",1);
        List<SundayProduct> textProducts=super.select(param);
        result.put("textProducts",textProducts);
        return result;

    }

    /**
     * 获取行业页面项目
     * @param parentId
     * @param secondId
     * @return
     */
    public Map<String,Object> selectSecondProduct(Long parentId,Long secondId){
        Map<String,Object> result=new HashMap<String,Object>();
        Map<String,Object> param=new HashMap<String,Object>();
        List<SundayProduct> brandHotProducts=null;
        List<SundayProduct> brandPaimingProducts=null;
        List<SundayProduct> brandGoodProducts=new ArrayList<SundayProduct>();
        List<SundayProduct> brandRecommendProducts=null;
        List<SundayProduct> brandNewProducts=null;
        List<SundayProduct> brandJingProducts=null;
        List<SundayProduct> brandTextProducts=null;
        if(LongUtil.isNotZreo(parentId)){
            //品牌热门项目
            param.clear();
            param.put("isBrandHot",1);
            param.put("parentId",parentId);
            param.put("row_start",0);
            param.put("row_size",12);
            brandHotProducts=super.select(param);
            //品牌推荐
            param.clear();
            param.put("isBrandRecommend",1);
            param.put("parentId",parentId);
            param.put("row_start",0);
            param.put("row_size",12);
            brandRecommendProducts=super.select(param);
            //品牌精选
            param.clear();
            param.put("isBrandJing",1);
            param.put("parentId",parentId);
            param.put("row_start",0);
            param.put("row_size",14);
            brandJingProducts=super.select(param);
            //品牌文字推荐
            param.clear();
            param.put("isBrandText",1);
            param.put("parentId",parentId);
            param.put("row_start",0);
            brandTextProducts=super.select(param);
            //最新
            param.clear();
            param.put("parentId",parentId);
            param.put("sort_name","ct");
            param.put("sort_order","desc");
            param.put("row_start",0);
            param.put("row_size",24);
            brandNewProducts=super.select(param);
            //获取行业自定义排名项目
            param.clear();
            param.put("parentId",parentId);
            param.put("sort_name","sequence");
            param.put("sort_order","desc");
            param.put("row_start",0);
            param.put("row_size",12);
            brandPaimingProducts=super.select(param);
            //获取该一级行业的优选项目
            param.clear();
            param.put("parentId",parentId);
            List<SundayProductCategory> categories=categoryService.select(param);
            if(ListUtil.isNotNull(categories)){
                for(SundayProductCategory category:categories){
                    param.clear();
                    param.put("isBrandGood",1);
                    param.put("categoryId",category.getId());
                    param.put("row_start",0);
                    param.put("row_size",6);
                    List<SundayProduct> products=super.select(param);
                    if(ListUtil.isNotNull(products)){
                        brandGoodProducts.addAll(products);
                    }
                }
            }

        }
        if(LongUtil.isNotZreo(secondId)){
            //品牌热门项目
            param.clear();
            param.put("isBrandHot",1);
            param.put("categoryId",secondId);
            param.put("row_start",0);
            param.put("row_size",12);
            brandHotProducts=super.select(param);
            //品牌推荐
            param.clear();
            param.put("isBrandRecommend",1);
            param.put("categoryId",secondId);
            param.put("row_start",0);
            param.put("row_size",12);
            brandRecommendProducts=super.select(param);
            //品牌精选
            param.clear();
            param.put("isBrandJing",1);
            param.put("categoryId",secondId);
            param.put("row_start",0);
            param.put("row_size",14);
            brandJingProducts=super.select(param);
            //品牌文字推荐
            param.clear();
            param.put("isBrandText",1);
            param.put("parentId",parentId);
            param.put("row_start",0);
            brandTextProducts=super.select(param);
            //最新
            param.clear();
            param.put("categoryId",secondId);
            param.put("sort_name","ct");
            param.put("sort_order","desc");
            param.put("row_start",0);
            param.put("row_size",24);
            brandNewProducts=super.select(param);
            //获取行业自定义排行项目
            param.clear();
            param.put("categoryId",secondId);
            param.put("sort_name","sequence");
            param.put("sort_order","desc");
            param.put("row_start",0);
            param.put("row_size",12);
            brandPaimingProducts=super.select(param);
            //获取该一级行业的优选项目
            SundayProductCategory category=categoryService.find(secondId);
            param.clear();
            param.put("parentId",category.getParentId());
            List<SundayProductCategory> categories=categoryService.select(param);
            if(ListUtil.isNotNull(categories)){
                for(SundayProductCategory category1:categories){
                    param.clear();
                    param.put("isBrandGood",1);
                    param.put("categoryId",category1.getId());
                    param.put("row_start",0);
                    param.put("row_size",6);
                    List<SundayProduct> products=super.select(param);
                    if(ListUtil.isNotNull(products)){
                        brandGoodProducts.addAll(products);
                    }
                }
            }
        }

        result.put("brandHotProducts",brandHotProducts);
        result.put("brandPaimingProducts",brandPaimingProducts);
        result.put("brandGoodProducts",brandGoodProducts);
        result.put("brandRecommendProducts",brandRecommendProducts);
        result.put("brandNewProducts",brandNewProducts);
        result.put("brandJingProducts",brandJingProducts);
        result.put("brandTextProducts",brandTextProducts);
        return result;

    }


    /**
     * 获取个人主页推荐项目
     * @param memberRecordId
     * @return
     */
    public Map<String,Object> selectMemberIndexProduct(Long memberRecordId){
        Map<String,Object> result=new HashMap<String,Object>();
        //关注的项目
        List<SundayProduct> favoriteProducts=null;
        //新加入的项目
        List<SundayProduct> newProducts=null;
        //适合的项目
        List<SundayProduct> fitProducts=null;
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted",0);
        param.put("memberRecordId",memberRecordId);
        List<JoinFavorite> favorites=favoriteService.list(param);
        List<Long> favoriteProductIds=new ArrayList<Long>();
        //关注的项目
        if(ListUtil.isNotNull(favorites)){
            for(JoinFavorite favorite:favorites){
                favoriteProductIds.add(favorite.getOtherId());
            }
            param.clear();
            param.put("ids",favoriteProductIds);
            favoriteProducts=super.select(param);
        }
        //适合我的项目
        SundayMemberRecord member=memberRecordService.getById(memberRecordId);
        param.clear();
        param.put("isDeleted",0);
        param.put("investAmount",member.getSelfAmount());
        param.put("notIds",favoriteProductIds);
        param.put("row_start",0);
        param.put("row_size",8);
        fitProducts=super.select(param);

        //最新项目
        param.clear();
        param.put("isDeleted",0);
        param.put("sort_name","ct");
        param.put("sort_order","desc");
        param.put("row_start",0);
        param.put("row_size",8);
        newProducts=super.select(param);

        result.put("favoriteProducts",favoriteProducts);
        result.put("newProducts",newProducts);
        result.put("fitProducts",fitProducts);
        return result;
    }

    /**
     * 获取用户关注的所有项目
     * @param memberRecordId
     * @return
     */
    public PageResult<SundayProduct> selectMemberFavoriteProduct(HttpServletRequest request,Long memberRecordId,Integer page){
        //关注的项目
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted",0);
        param.put("memberRecordId",memberRecordId);
        List<JoinFavorite> favorites=favoriteService.list(param);
        List<Long> favoriteProductIds=new ArrayList<Long>();
        //关注的项目
        if(ListUtil.isNotNull(favorites)){
            for(JoinFavorite favorite:favorites){
                favoriteProductIds.add(favorite.getOtherId());
            }
            param.clear();
            param.put("ids",favoriteProductIds);
        }else {
            return new PageResult<SundayProduct>();
        }

        if(page!=null&&page!=0){
            param.put("row_start",(page-1)* SundayCommonConstants.row_size);
        }else {
            param.put("row_start",0);
        }
        param.put("row_size",SundayCommonConstants.row_size);

        return new PageResult<SundayProduct>(super.select(param),super.count(param));



    }
}
